package cece.service.impl;

import cece.entity.Movie;
import cece.entity.PageBean;
import cece.mapper.MovieMapper;
import cece.service.MovieService;
import cece.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageBean<Movie> findAll(Integer currentPage, Integer pageSize) {
        Integer startPage = (currentPage - 1) * pageSize;
        List<Movie> movies = movieMapper.findAll(startPage, pageSize);
        PageBean<Movie> pageBean = new PageBean<>();
        Integer totalCount = movieMapper.findTotalCount();
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
        pageBean.setMovies(movies);
        pageBean.setCurrentPage(currentPage);
        Integer totalPage = (totalCount / pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public List<Movie> finScore(Integer currentPage, Integer pageSize) {
        Integer startPage = (currentPage - 1) * pageSize;
        List<Movie> movies = movieMapper.findScore(startPage, pageSize);
        return movies;
    }

    @Override
    public Movie findById(Integer mid) {
        Movie movie = movieMapper.findById(mid);
        if (null != movie) {
            return movie;
        }
        return null;
    }


}
